import {
  IsArray,
  IsBoolean,
  IsDateString,
  IsNumber,
  IsObject,
  IsOptional,
  IsString,
} from "class-validator";
import { TwikooConfiguration } from "./../../../ui_console/src/api/configuration";
import {
  ArticleConfiguration,
  BaiduTongjiConfiguration,
  EmailConfiguration,
  ResourceConfiguration,
  RSSConfiguration,
  ServerConfiguration,
  UserConfiguration,
  WebsiteConfiguration,
  WechatConfiguration,
} from "src/common/configuration";

export class WebsiteConfigurationDTO implements Partial<WebsiteConfiguration> {
  @IsString()
  @IsOptional()
  logo?: string;

  @IsString()
  @IsOptional()
  name?: string;

  @IsString()
  @IsOptional()
  description?: string;

  @IsString()
  @IsOptional()
  url?: string;

  @IsString()
  @IsOptional()
  version?: string;

  @IsString()
  @IsOptional()
  copyright?: string;

  @IsDateString()
  @IsOptional()
  setUpTime?: string | Date;

  @IsDateString()
  @IsOptional()
  startRunningTime?: string | Date;

  @IsString()
  @IsOptional()
  ICP备案?: string;

  @IsString()
  @IsOptional()
  公网安备案?: string;

  @IsString()
  @IsOptional()
  萌ICP备案号?: string;
}
export class EmailConfigurationDTO implements Partial<EmailConfiguration> {
  @IsBoolean()
  @IsOptional()
  enable?: boolean;

  @IsString()
  @IsOptional()
  host?: string;

  @IsNumber()
  @IsOptional()
  port?: number;

  @IsBoolean()
  @IsOptional()
  secure?: boolean; // Use `true` for port 465, `false` for all other ports

  @IsString()
  @IsOptional()
  user?: string;

  @IsString()
  @IsOptional()
  pass: string;
}

export class UserConfigurationDTO implements Partial<UserConfiguration> {
  // 管理员用户名
  @IsArray()
  @IsOptional()
  administrators?: string[];
  // 站长信息
  @IsObject()
  @IsOptional()
  master?: {
    nickname?: string;
    username?: string;
    emails?: string[];
  };
}
export class ArticleConfigurationDTO implements Partial<ArticleConfiguration> {
  // 归档数据更新时间间隔（分钟）
  @IsNumber()
  @IsOptional()
  archiveUpdateInterval?: number;
  // 点赞功能
  @IsBoolean()
  @IsOptional()
  enableLike?: boolean;

  // 评论功能
  @IsBoolean()
  @IsOptional()
  enableComment?: boolean;

  @IsBoolean()
  @IsOptional()
  enableCommentReview?: boolean;
}
export class RSSConfigurationDTO implements Partial<RSSConfiguration> {
  @IsBoolean()
  @IsOptional()
  enable?: boolean;

  // RSS文件更新时间间隔（分钟）
  @IsNumber()
  @IsOptional()
  updateInterval?: number;
}
// twikoo 评论系统配置
// 参考 https://twikoo.js.org/frontend.html
export class TwikooConfigurationDTO implements Partial<TwikooConfiguration> {
  @IsBoolean()
  @IsOptional()
  enable?: boolean;

  @IsString()
  @IsOptional()
  envID?: string;

  @IsString()
  @IsOptional()
  region?: "ap-shanghai" | "ap-guangzhou" | "";

  @IsString()
  @IsOptional()
  lang?: "zh" | "zh-cn" | "zh-hk" | "zh-tw" | "en" | "en-us" | "uz" | "ja";
}
// 百度统计相关配置
export class BaiduTongjiConfigurationDTO
  implements Partial<BaiduTongjiConfiguration>
{
  @IsString()
  @IsOptional()
  domain?: string;

  @IsString()
  @IsOptional()
  code?: string;
}
// 微信公众号API相关配置
export class WechatConfigurationDTO implements Partial<WechatConfiguration> {
  @IsString()
  @IsOptional()
  appID?: string;

  @IsString()
  @IsOptional()
  appSecret?: string;
}
// 服务相关配置
export class ServerConfigurationDTO implements Partial<ServerConfiguration> {
  @IsNumber()
  @IsOptional()
  port?: number;

  @IsObject()
  @IsOptional()
  https?: {
    enable: boolean;
    http2: boolean;
    certs?: {
      // 带 default 则 匹配不到时默认使用
      servername: string[];
      cert: string;
      key: string;
    }[];
  };
}
// 内容资源相关
export class ResourceConfigurationDTO
  implements Partial<ResourceConfiguration>
{
  rootDirectoryPath: string;
  imageDirectoryPath: string;
  videoDirectoryPath: string;
  audioDirectoryPath: string;
  articleDirectoryPath: string;
  RSSDirectoryPath: string;
  RSSFilePath: string;
}

export class ConfigurationDTO {
  // 网站相关配置
  website?: WebsiteConfiguration | WebsiteConfigurationDTO;
  email?: EmailConfiguration | EmailConfigurationDTO;
  user?: UserConfiguration | UserConfigurationDTO;
  article?: ArticleConfiguration | ArticleConfigurationDTO;
  rss?: RSSConfiguration | RSSConfigurationDTO;
  // twikoo 评论系统配置
  // 参考 https://twikoo.js.org/frontend.html
  twikoo?: TwikooConfiguration | TwikooConfigurationDTO;
  // 统计相关配置
  tongji?: {
    // 百度统计相关配置
    baidu: BaiduTongjiConfiguration[] | BaiduTongjiConfigurationDTO[];
  };
  // 微信公众号API相关配置
  wechat?: WechatConfiguration | WechatConfigurationDTO;
  // 服务相关配置
  server?: ServerConfiguration | ServerConfigurationDTO;
  // 内容资源相关
  resource?: ResourceConfiguration | ResourceConfigurationDTO;
}
